package com.data;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DocReadCOM {

	public static DocReadCOM docReadCom = new DocReadCOM();
	static ArrayList<Integer> idList = new ArrayList<Integer>();
	static ArrayList<String> dateList = new ArrayList<String>();
	static ArrayList<Integer> tempList = new ArrayList<Integer>();
	static ArrayList<Integer> standardPressureValueList = new ArrayList<Integer>();
	static ArrayList<Integer> realPressureValueList = new ArrayList<Integer>();
	static ArrayList<Float> standardCurrentValueList = new ArrayList<Float>();
	static ArrayList<Float> realCurrentValueList = new ArrayList<Float>();

	public static void convert(String fileName, boolean validate) {
		String uri = "file:" + new File(fileName).getAbsolutePath();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Document doc = null;
		try {
			builder = factory.newDocumentBuilder();
			doc = builder.parse(uri);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		doRecursive(doc);

	}

	private static void doRecursive(Node p) {
		if (p == null) {
			return;
		}
		NodeList nodes = p.getChildNodes();
		for (int i = 0; i < nodes.getLength(); i++) {
			Node n = nodes.item(i);
			if (n == null) {
				continue;
			}
			doNode(n);
		}
	}

	private static void doNode(Node n) {
		switch (n.getNodeType()) {
		case Node.ELEMENT_NODE:
			System.out.println("ELEMENT<" + n.getNodeName() + ">");
			doRecursive(n);
			break;
		case Node.TEXT_NODE:
			if (n.getNodeName().equals("id")) {
				idList.add(Integer.parseInt(n.getNodeValue()));
			}
			if (n.getNodeName().equals("date")) {
				dateList.add(n.getNodeValue());
			}
			if (n.getNodeName().equals("temp")) {
				tempList.add(Integer.parseInt(n.getNodeValue()));
			}
			if (n.getNodeName().equals("standardPressureValue")) {
				standardPressureValueList.add(Integer.parseInt(n.getNodeValue()));
			}
			if (n.getNodeName().equals("realPressureValue")) {
				realPressureValueList.add(Integer.parseInt(n.getNodeValue()));
			}
			if (n.getNodeName().equals("standardCurrentValue")) {
				standardCurrentValueList.add(Float.parseFloat(n.getNodeValue()));
			}
			if (n.getNodeName().equals("realCurrentValue")) {
				realCurrentValueList.add(Float.parseFloat(n.getNodeValue()));
			}
			break;
		default:
			break;
		}
	}
}
